package com.nageoffer.subject.application.controller;

import cn.hutool.core.util.StrUtil;
import com.nageoffer.subject.application.convert.SubjectLabelDTOConverter;
import com.nageoffer.subject.application.dto.SubjectLabelDTO;
import com.nageoffer.subject.common.entity.Result;
import com.nageoffer.subject.domain.entity.SubjectLabelBO;
import com.nageoffer.subject.domain.service.SubjectLabelDomainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 标签控制层
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class SubjectLabelController {
    private final SubjectLabelDomainService subjectLabelDomainService;

    /**
     * 新增标签
     */
    @PostMapping("/subject/label/addLabel")
    public Result<Boolean> addLabel(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (StrUtil.isBlank(subjectLabelDTO.getLabelName())) {
                return Result.fail("标签名不能为空");
            }
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE
                    .convertDTOTOLabelBO(subjectLabelDTO);
            Boolean result = subjectLabelDomainService.addLabel(subjectLabelBO);
            return Result.success(result);
        } catch (Throwable ex) {
            return Result.fail("新增标签失败");
        }
    }

    /**
     * 更新标签
     */
    @PostMapping("/subject/label/updateLabel")
    public Result<Boolean> updateLabel(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (Objects.isNull(subjectLabelDTO.getId())) {
                return Result.fail("标签ID不能为空");
            }
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE
                    .convertDTOTOLabelBO(subjectLabelDTO);
            Boolean result = subjectLabelDomainService.updateLabel(subjectLabelBO);
            return Result.success(result);
        } catch (Throwable ex) {
            return Result.fail("更新标签失败");
        }
    }

    /**
     * 删除标签
     */
    @PostMapping("/subject/label/deleteLabel")
    public Result<Boolean> deleteLabel(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (Objects.isNull(subjectLabelDTO.getId())) {
                return Result.fail("标签ID不能为空");
            }
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE
                    .convertDTOTOLabelBO(subjectLabelDTO);
            Boolean result = subjectLabelDomainService.deleteLabel(subjectLabelBO);
            return Result.success(result);
        } catch (Throwable ex) {
            return Result.fail("删除标签失败");
        }
    }


    /**
     * 查询分类下标签
     */
    @PostMapping("/subject/label/queryLabelByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (Objects.isNull(subjectLabelDTO.getCategoryId())) {
                return Result.fail("分类ID不能为空");
            }
            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.INSTANCE
                    .convertDTOTOLabelBO(subjectLabelDTO);
            List<SubjectLabelBO> subjectLabelBOList = subjectLabelDomainService.queryLabelByCategoryId(subjectLabelBO);
            List<SubjectLabelDTO> subjectLabelDTOList = SubjectLabelDTOConverter.INSTANCE
                    .convertBOListTOLabelDTOList(subjectLabelBOList);
            return Result.success(subjectLabelDTOList);
        } catch (Throwable ex) {
            return Result.fail("查询分类下标签失败");
        }
    }

}
